package com.uw.tookit;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CategoriesActivity extends ListActivity {
	
	/**
	 * This activity handles the loading of the category page of the application.
	 * A list of categories of search options is displayed
	 */
	/*
	 * Called when the activity is first created.
	 */
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	String[] categoryItems = getResources().getStringArray(R.array.categories_array);
    	setListAdapter(new ArrayAdapter<String>(this, R.layout.categories, categoryItems));
    	
    	ListView lv = getListView();
    	lv.setTextFilterEnabled(true);
    	// An action listener is placed on the items in the category list, and only if the user
    	// clicked on "Computer Labs" is another page of computer labs displayed.
    	lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
				String categoryClicked = ((TextView) view).getText().toString();
				makeServerRequest(categoryClicked);
			}
      	});
    }
	
	private void makeServerRequest(String category) {
		ArrayList<String> paramsNames = new ArrayList<String>();
    	ArrayList<String> paramsValues = new ArrayList<String>();
    	paramsNames.add("category");
    	paramsValues.add(category);
    	// Create the results activity, and pass on the parameter-value parameters for it to query.
    	Intent resultsPage = new Intent(CategoriesActivity.this, Results.class);
        resultsPage.putStringArrayListExtra("paramNames", paramsNames);
        resultsPage.putStringArrayListExtra("paramValues", paramsValues);
        startActivityForResult(resultsPage, 0);
	}
}